#include <stdio.h>
#include "cmsis_os2.h"
#include "los_task.h"
#include "ohos_init.h"
#include "at32f403a_407.h"
#include <string.h>
#include "usart1.h" //usart1串口通信的部分
#include "bsp_beep.h"
#include "protocol.h"

/********************************************************************************************************/
/* 解析上位机发送过来的命令, 并处理控制的内容 */
void vTask_Control(void)
{
    while (1)
    {
        if (Get_CMD_Flag())
        {
            Upper_Data_Parse(Get_RxBuffer(), Get_CMD_Length());
            Clear_CMD_Flag();
        }
        osDelay(1);
    }
}
/**
 * 创建任务
 * @param func 任务函数
 * @param name 任务名
 * @param stack_size 堆栈大小
 * @param priority 优先级
 * @param tid_out 任务ID
 */
static void VTask_Create(osThreadFunc_t func, const char *name, uint32_t stack_size, osPriority_t priority, osThreadId_t *tid_out)
{
    osThreadAttr_t attr;
    attr.name = name;
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = stack_size; // 堆栈大小调大一点
    attr.priority = priority;
    osThreadId_t tid = osThreadNew(func, NULL, &attr);
    if (tid_out)
        *tid_out = tid;
    if (tid == NULL)
    {
        printf(" [%d] User_main_task_create create %s: NG\n", tid, name);
    }
    else
    {
        printf(" [%d]User_main_task_create create %s: OK\n", tid, name);
    }
}
void User_main_task_create(void)
{
    /****************************************************/
    ctrl_uart1_init();
    /****************************************************/
    // 初始化板载蜂鸣器
    beep_init();
    /****************************************************/
    VTask_Create(vTask_Control, "vTask_Control", 1024 * 20, osPriorityLow + 7,NULL);
}
APP_FEATURE_INIT(User_main_task_create);
